package com.mini.demos.webgame.codec;

import org.apache.mina.core.buffer.IoBuffer;

import com.mini.demos.webgame.msg.AbstractGameMsg;
import com.mini.demos.webgame.msg.JobMsg;
import com.mini.demos.webgame.msg.MsgHead;
import com.mini.demos.webgame.util.CommandConstants;
import com.mini.demos.webgame.util.Constants;

public class JobDecoder extends AbstractDecoder {

	@Override
	public AbstractGameMsg decodeBody(IoBuffer buffer, MsgHead head) {
		JobMsg jobMsg = new JobMsg();
		jobMsg.setHead(head);
		jobMsg.setJobId(buffer.getInt());
		jobMsg.setJobType(buffer.getInt());
		jobMsg.setCostPrice(buffer.getInt());
		jobMsg.setCostTime(buffer.getInt());
		jobMsg.setGetPrice(buffer.getInt());
		try{
			String body = buffer.getString(Constants.charset.newDecoder());
			String[] bodys = body.split(CommandConstants.COMMAND_SPLIT);
			jobMsg.setUserName(bodys[0]);
			jobMsg.setJobItemPath(bodys[1]);
		}catch(Exception e){
			e.printStackTrace();
		}
		return jobMsg;
	}

	@Override
	public boolean isLegalCommand(int commandId) {
		if(commandId == CommandConstants.JOB){
			return true;
		}
		return false;
	}

}
